package cn.piesat.scanning.business.xz.service.forimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import cn.piesat.scanning.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildXIZANG_FOR_DIFFJServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
//森林去年同期算法
        // BAS_NDVI_540000_L3_FY3D_MERSI_1000M_GLL_MOAM_20210701000000_20210731000000.TIFF
        Map<String, Object> params = new HashMap<>();
        params.put("primaryFile", primaryFile);
        String fileName = primaryFile.substring(primaryFile.lastIndexOf("/") + 1);
        String fileType = primaryFile.substring(primaryFile.lastIndexOf(".") + 1);
        String[] s1 = fileName.split("_");
        String moduleIid = s1[0];
        String level = s1[3];
        String satellite = s1[4];
        String productMode = s1[8];
        String region = s1[2];
        String dateStr = DateUtils.getDateStr(primaryFile, "(\\d{14})");
        Date date = DateUtil.parse(dateStr, "yyyyMMddHHmmss");
        Date dateTime = DateUtil.offsetMonth(date, -12);
        String dateTimeStr = DateUtil.format(dateTime, "yyyyMMddHHmmss");
        HtScanningMetadataDTO metadataDTO = htScanningMetadataService.findMetadataByParam
                (dateTimeStr, level, satellite, taskPathParam.getParameter04(), region, productMode, moduleIid, fileType);
        if (null == metadataDTO) {
            algorithmParams.put("parameter01", null);
        } else {
            params.put("lastFile", taskPathParam.getParameter02() + metadataDTO.getFilePath());
            params.put("auxPath", taskPathParam.getParameter03());
            params.put("satelliteName", satellite);
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
